home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15178 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: mayne.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Integers -> Pointers -> Integers
  5. Date: 16 Apr 1996 12:58:54 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4l0u5uINN6sd@mayne.ugrad.cs.ubc.ca>
  8. References: <DpoyFt.Ls2@uns.bris.ac.uk>
  9. NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
  10.  
  11. In article <DpoyFt.Ls2@uns.bris.ac.uk>,
  12. Chris Davis-Pipe <chris@pact.srf.ac.uk> wrote:
  13. >Hi,
  14. >
  15. >Suppose i have a pointer to a struture.
  16. >
  17. >Is there a way that I can store that pointer and others like it in an array
  18. >of integers ?
  19.  
  20. Why not use an array of the appropriate pointer types?
  21.  
  22. >Obviously, at some point I am going to want to convert the entry in the array
  23. >back to a pointer to the structure.
  24.  
  25. If you are looking for a way to set up a generic array that can hold any type,
  26. make it an array of:
  27.  
  28.     union data {
  29.         void *p;
  30.         unsigned long u;
  31.         long i;
  32.         double d;
  33.     }
  34.  
  35. Now if you make an array A[] of union data, then you can do these assignments
  36. with confidence:
  37.  
  38. A[3].d    = 4.00;
  39. A[4].p    = "String";
  40. A[5].i    = 3;
  41.  
  42. and so on.
  43. -- 
  44. I'm not really a jerk, but I play one on Usenet.
  45.